--[[
	invoke boot sequence
	See Copyright Notice in "invoke.h"
]]

invoke._mt_func = {
	__call = function(table, ...)
		return invoke.call(table.sym, table.sign, unpack(arg))
	end
}	

invoke._mt_handle = {
	__gc = function(handle)
		invoke.close(handle)
	end,

	__index = function(userdata, key)
		if key == "import" then
			return invoke.import
		elseif key == "close" then
			return invoke.close
		end
	end
}

invoke._mt_callback = {
	__gc = function(callback)
		invoke.cb_unregister(callback)
	end
}

invoke._cb_slots = {}

function invoke.import(handle, name, sign)
	local f = {
		handle = handle,
		sym = invoke.sym(handle, name),
		sign = sign
	}
	setmetatable(f, invoke._mt_func)
	return f
end

function invoke.callback(sign, fn)
	cb, slot = invoke.cb_register(sign)
	invoke._cb_slots[slot] = fn
	return cb
end
